<?php

    class CicpDrawBoard implements Cicp_Handler_Interface
{
    private $_width = 0;
    private $_height = 0;
    private $_alphabet = array();

    function initGraph($x, $y, $z)
    {
        $graph = new Cicp_Graph();
        $graph->setPosition($x, $y, $z);

        $this->_width = 100;
        $this->_height = 10;

        $this->createMatrix($graph);
        $this->drawText($graph, "Hei Erwin!!!");

        return $graph;
    }

    function menuChoice(Cicp_Graph $graph, Cicp_Graph_Node $node, Cicp_Graph_Menu $menu, $menuButtonLabel)
    {
        switch ($menuButtonLabel) {
        case 'Redraw':
            $this->drawText($graph, "!");
            break;
        case 'Clear':
            $this->clear($graph);
            break;
        case 'Live-Toggle':
            $this->clear($graph);
            $this->drawText($graph, "Cicp " . rand(1000, 9999));
            break;
        }
        return $graph;
    }

    function drawText($graph, $text)
    {
        // init alphabet if neccessary
        if (count($this->_alphabet) == 0) {
            $this->defineAlphabet();
        }

        $offsetY = 1;
        $offsetZ = 1;
        $this->clear($graph);
        $text = strtolower($text);
        // // check where to put line breaks
        // $words = explode(' ', $text);
        // $revText = '';
        // $displayWidth = $this->_width - $offsetY*2;
        // $textPosition = 0;
        // $restWidth = $displayWidth;
        // foreach ($words as $index => $word) {
        //     $wordWidth = strlen($word) * 6 - 1;
        //     // check if $word fits still on this line
        //     if ($wordWidth > $restWidth) {
        //         if ($wordWidth > $restWidth) {
        //             // $word is too big for display
        //             while ($wordWidth > $displayWidth) {
        //                 $revText .= substr($word, 0, $restWidth - 1);
        //                 $revText .= "-\n";
        //                 $word = substr($word, $restWidth);
        //                 $wordWidth = strlen($word);
        //                 $restWidth = $displayWidth;
        //             }
        //         }
        //         // add a new line, then add word
        //         $revText .= "\n$word ";
        //         $restWidth .= $displayWidth - $wordWidth - 1;
        //     }
        //     else {
        //         // it fits, simply add it
        //         $revText .= "$word ";
        //         // calculate rest width
        //         $restWidth = $restWidth - $wordWidth - 1;
        //     }
                     
        // }
        
        // $text = $revText;
        $strLen = strlen($text);
        for ($i = 0; $i < $strLen; $i++) {
            // see if char exists
            if (!isset($this->_alphabet[$text{$i}])) {
                continue;
            }
            // check for new line char
            if ($text{$i} == "\n") {
                $offsetZ = $offsetZ + 9; // 7(char height) + 2(line
                                         // spacing)
                continue;
            }
            $charCode = $this->_alphabet[$text{$i}];
            for ($l = 0; $l < 7; $l++) {
                for ($c = 0; $c < 5; $c++) {
                    if ($charCode{5 * $l + $c} == '1') {
                        $node = $graph->getNodeByIndex(0, $i * 6 + $c + $offsetY, $l+1 + $offsetZ);
                        if ($node instanceof Cicp_Graph_Node) {
                            // flip colors
                            $nodeHlColor = $node->getNodeProperty('hl_color');
                            $nodeColor = $node->getNodeProperty('color');
                            $node->setColor($nodeHlColor->r, $nodeHlColor->g, $nodeHlColor->b);
                            $node->setHlColor($nodeColor->r, $nodeColor->g, $nodeColor->b);
                        }
                    }
                }
            }
        }
    }

    function clear($graph)
    {
        $nodes = $graph->getNodes();
        foreach ($nodes as $node) {
            $node->setColor(192, 222, 237);
            $node->setHlColor(255, 255, 255);
        }
    }

    function createMatrix($graph)
    {
        // creat menu
        $menu = new Cicp_Graph_Menu;
        $menu->addButton('Delete', Cicp_Graph_Menu::BUTTON_DELETE);
        $menu->addButton('Invert', Cicp_Graph_Menu::BUTTON_HL_RESET);
        $menu->addButton('Redraw');
        $menu->addButton('Live-Toggle', Cicp_Graph_Menu::BUTTON_LIVE);
        $menu->addButton('Clear');
        $graph->addMenu($menu);

        // create board of 50x10 pixels
        $pixelNode = new Cicp_Graph_Node;
        $pixelNode->setShape('rec');
        $pixelNode->setTitle("\t");
        $pixelNode->setDimensions(0.01, 0.02, 0); // cube of 0.02s
        $pixelNode->setColor(192, 222, 237); // black
        $pixelNode->setHlColor(255, 255, 255); // orange
        $pixelNode->assignMenu($menu);

        $offset = new Cicp_Vector3d_Coordinate(0, 0, 0);

        $width = $this->_width;
        $height = $this->_height;
        $nodeDim = $pixelNode->getNodeProperty('loc2');
        for ($w = 1; $w <= $width; $w++) {
            for ($h = 1; $h <= $height; $h++) {
                $node = clone $pixelNode;
                $node->setPosition($offset->x,
                                   $offset->y + $h * $nodeDim->y,
                                   $offset->z + $w * $nodeDim->y);
                $node->setIndex(0, $w, $height - $h + 1);
                $graph->registerNode($node);
            }
        }
    }

    function defineAlphabet()
    {
        $this->_alphabet = array(
            'a' => '01110' . '10001' . '10001' . '10001' . '11111' . '10001' . '10001',
            'b' => '11110' . '10001' . '10001' . '11110' . '10001' . '10001' . '11110',
            'c' => '01110' . '10001' . '10000' . '10000' . '10000' . '10001' . '01110',
            'd' => '11110' . '10001' . '10001' . '10001' . '10001' . '10001' . '11110',
            'e' => '11111' . '10000' . '10000' . '11110' . '10000' . '10000' . '11111',
            'f' => '11111' . '10000' . '10000' . '11110' . '10000' . '10000' . '10000',
            'g' => '01110' . '10001' . '10000' . '10111' . '10101' . '10001' . '01110',
            'h' => '10001' . '10001' . '10001' . '11111' . '10001' . '10001' . '10001',
            'i' => '01110' . '00100' . '00100' . '00100' . '00100' . '00100' . '01110',
            'j' => '00111' . '00010' . '00010' . '00010' . '00010' . '10010' . '01100',
            'k' => '10001' . '10010' . '10100' . '11000' . '10100' . '10010' . '10001',
            'l' => '10000' . '10000' . '10000' . '10000' . '10000' . '10000' . '11111',
            'm' => '10001' . '11011' . '10101' . '10101' . '10001' . '10001' . '10001',
            'n' => '10001' . '10001' . '11001' . '10101' . '10011' . '10001' . '10001',
            'o' => '01110' . '10001' . '10001' . '10001' . '10001' . '10001' . '01110',
            'p' => '11110' . '10001' . '10001' . '11110' . '10000' . '10000' . '10000',
            'q' => '01110' . '10001' . '10001' . '10001' . '10001' . '01110' . '00010',
            'r' => '11110' . '10001' . '10001' . '11110' . '10100' . '10010' . '10001',
            's' => '01110' . '10001' . '10000' . '01110' . '00001' . '10001' . '01110',
            't' => '11111' . '00100' . '00100' . '00100' . '00100' . '00100' . '00100',
            'u' => '10001' . '10001' . '10001' . '10001' . '10001' . '10001' . '10001',
            'v' => '10001' . '10001' . '10001' . '10001' . '10001' . '01010' . '00100',
            'w' => '10001' . '10001' . '10001' . '10101' . '10101' . '10101' . '01010',
            'x' => '10001' . '10001' . '01010' . '00100' . '01010' . '10001' . '10001',
            'y' => '10001' . '10001' . '01010' . '00100' . '00100' . '00100' . '00100',
            'z' => '11111' . '00001' . '00010' . '00100' . '01000' . '10000' . '11111',
            '1' => '00100' . '01100' . '10100' . '00100' . '00100' . '00100' . '11111',
            '2' => '01110' . '10001' . '00001' . '00010' . '00100' . '01000' . '11111',
            '3' => '01110' . '10001' . '00001' . '00110' . '00001' . '10001' . '01110',
            '4' => '00010' . '00110' . '01010' . '10010' . '11111' . '00010' . '00010',
            '5' => '11111' . '10000' . '11110' . '00001' . '00001' . '10001' . '01110',
            '6' => '01110' . '10001' . '10000' . '11110' . '10001' . '10001' . '01110',
            '7' => '11111' . '00001' . '00010' . '00100' . '00100' . '00100' . '00100',
            '8' => '01110' . '10001' . '10001' . '01110' . '10001' . '10001' . '01110',
            '9' => '01110' . '10001' . '10001' . '01111' . '00001' . '10001' . '01110',
            '0' => '01110' . '10001' . '10011' . '10101' . '11001' . '10001' . '01110',
            '.' => '00000' . '00000' . '00000' . '00000' . '00000' . '00000' . '00100',
            ' ' => '00000' . '00000' . '00000' . '00000' . '00000' . '00000' . '00000',
            '!' => '00100' . '00100' . '00100' . '00100' . '00100' . '00000' . '00100',
            ',' => '00000' . '00000' . '00000' . '00000' . '00100' . '01000' . '10000',
            ':' => '00000' . '00000' . '00100' . '00000' . '00100' . '00000' . '00000',
                                 );
    }
}